import { loginRequest } from './index'

export function getLoginCode() {
  return new Promise((resolve, reject) => {
    wx.login({
      timeout: 1000,
      success: (res) => {
        resolve(res.code)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

export function codeToToken(code) {
  return loginRequest.post('/login', {code})
}

export function checkToken() {
  return loginRequest.post('/auth', {}, true)
}

export function checkSession() {
  return new Promise((resolve, reject) => {
    wx.checkSession({
      success: (res) => {
        if (res.errMsg === 'checkSession:ok') {
          resolve(true)
        } else {
          resolve(false)
        }
      },
      fail: (res) => {
        reject(res)
      }
    })
  })
}

export function getUserInfo() {
  return new Promise((resolve, reject) => {
    wx.getUserProfile({
      desc: '获取您的用户信息',
      success: resolve,
      fail: reject
    })
  })
}